<?php
namespace Erp\Controller;
use Think\Controller;
use Fn;
/**
 * 运输方式Model
 * @author Administrator
 */
class OrdersShippingMethodController extends Controller {
    public function _getShippingMethod($code=false,$error = false)
    {
        static $__mList;
        if(empty($__mList)){
            $table = 'orders_shipping_method';
            $map = array('shipping_method_status'=>9);
            $method = 'select';
            $data = '';
            $tmp = A('OrdersInfo')->Order($table,$map,$method,$data);
            $tmp = json_decode($tmp,true);
            $__mList = Fn\Arr::neaten($tmp,'','shipping_method_code');
        }
        return $code===false? $__mList:( isset($__mList[$code])?$__mList[$code]: $error);
    }
    /**
     * 获取到已开启的运输方式
     */
    public function getShippingMethod()
    {
        return Fn\Arr::resetKey($this->_getShippingMethod());
    }
    public function getShippingTypeByCode($code,$error=false)
    {
        $tmp = $this->_getShippingMethod($code);
        return ($tmp? $tmp['shipping_method_type']:$error); 
    }
    public function getShippingMethodByCode($code,$error=false){
        $tmp = $this->_getShippingMethod($code);
        return ($tmp? $tmp['shipping_method_name']:$error);
    }
    public function isEnabled($code){
        $r = $this->_getShippingMethod($code);
        return ($r && !empty($r));
    }

     /**
     * 根据条件获取单条记录指定字段的值
     * @param type $where 条件
     * @param type $field   要返回的值字段
     * @return type
     */
    public function getShippingByField($where = array()) {
        $table = 'orders_shipping_method';
        $map = $where;
        $method = 'find';
        $data = '';
        $shippingMethod = A('OrdersInfo')->Order($table,$map,$method,$data);
        $shippingMethod = json_decode($shippingMethod,true);
        if (!empty($shippingMethod)) {
            return $shippingMethod;
        } else {
            return null;
        }
    }

    // 所有运输方式，不管启用还是不启用
    public function getAllMethods(){
        $table = 'orders_shipping_method';
        $method = 'select';
        $map = array();
        $data = '';
        $res = A('OrdersInfo')->Order($table,$map,$method,$data);
        $res = json_decode($res,true);
        return $res;
    }

    // 所有启用的运输方式，仅code和name两个字段值
    public function getAllEnableMethods(){
        $table = 'orders_shipping_method';
        $method = 'select';
        $map['shipping_method_status'] = array('eq', '9');
        $data = '';
        $res = A('OrdersInfo')->Order($table,$map,$method,$data);
        $res = json_decode($res,true);
        return $res;
    }
	
	// 订单管理 -> 推广订单 获取启用的并且是TN的运输方式
	public function getEnableTnMethods(){
        $table = 'orders_shipping_method';
        $method = 'select';
		$map = array();  
        $map['shipping_method_status'] = 9;
        $map['shipping_method_code'] = array('like', '%TN');
        $data = '';
        $result = A('OrdersInfo')->Order($table,$map,$method,$data);
        $result = json_decode($result,true);
        // 特例
        array_push($result, array(
            'shipping_method_code' => 'DGPT',
            'shipping_method_name' => '东莞中邮小包平邮'
        ));
        array_push($result, array(
            'shipping_method_code' => 'YWMY',
            'shipping_method_name' => '燕文马邮小包平邮'
        ));
        array_push($result, array(
            'shipping_method_code' => 'TRPY',
            'shipping_method_name' => '燕文土耳其小包平邮'
        ));
        array_push($result, array(
            'shipping_method_code' => 'DGWISH',
            'shipping_method_name' => '东莞wish邮平邮'
        ));
        return $result;
	}
}
?>
